'''
Created on Apr 19, 2009

@author: Administrator
'''
from django.utils.translation import ugettext_lazy as _
from com.ericyang.groupshopping.company.models import Company
from django import forms
from com.ericyang.common.web.middleware import threadlocals

class CompanyCreationForm(forms.ModelForm):
    '''
    form of company creation
    '''
    name = forms.CharField(label=_("name"), max_length=50)
    type = forms.CharField(label=_("type"), max_length=75)
    address = forms.CharField(label=_("address"), max_length=200)
    postnum = forms.CharField(label=_("postnum"), max_length=20)
    phone = forms.CharField(label=_("phone"), max_length=30)
    email = forms.EmailField(label=_("email"), max_length=75)
    introduction = forms.CharField(label=_("introduction"), max_length=4000, widget=forms.Textarea)
    
    faxno = forms.CharField(label=_("faxno"), max_length=30)
    homepage = forms.CharField(label=_("homepage"), max_length=200)
    
    class Meta:
         model = Company
         fields = ("name", 'type', 'address', 'postnum', 'phone', 'faxno', 'introduction', 'email', 'homepage')

    def save(self, commit=True):
        company = super(CompanyCreationForm, self).save(commit=False)
        company.user = threadlocals.get_current_user()

        if commit:
            company.save()
        return company

class CompanyChangeForm(forms.ModelForm):
    '''
    form of company change
    '''
    name = forms.CharField(label=_("name"), max_length=50)
    type = forms.CharField(label=_("type"), max_length=75)
    address = forms.CharField(label=_("address"), max_length=200)
    postnum = forms.CharField(label=_("postnum"), max_length=20)
    phone = forms.CharField(label=_("phone"), max_length=30)
    email = forms.EmailField(label=_("email"), max_length=75)
    introduction = forms.CharField(label=_("introduction"), max_length=4000, widget=forms.Textarea)
    
    faxno = forms.CharField(label=_("faxno"), max_length=30)
    homepage = forms.CharField(label=_("homepage"), max_length=200)
    
    class Meta:
         model = Company
         fields = ("name", 'type', 'address', 'postnum', 'phone', 'faxno', 'introduction', 'email', 'homepage')
  
    def __init__(self, company, *args, **kwargs):
        self.company = company
        super(CompanyChangeForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        self.company.name = self.cleaned_data['name']
        self.company.type = self.cleaned_data['type']
        self.company.address = self.cleaned_data['address']
        self.company.postnum = self.cleaned_data['postnum']
        self.company.phone = self.cleaned_data['phone']
        self.company.faxno = self.cleaned_data['faxno']
        self.company.introduction = self.cleaned_data['introduction']
        self.company.email = self.cleaned_data['email']
        self.company.homepage = self.cleaned_data['homepage']
        if commit:
             self.company.save()
        return  self.company
    